package com.common.data2xml;

import java.io.IOException;
import java.util.List;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.common.manager.BaseManager;
import com.framework.model.IData;
import com.framework.model.impl.DataMap;

public class GenerateTreeXML {
	protected static Log log = LogFactory.getLog(GenerateTreeXML.class);
	
	// XML 文档对象
	private static Document document;

	// XML 文档根节点
	private  Element root;
	

	public GenerateTreeXML(String rootName,String node,String selFlag) {
		if(node==null){
			node="root";
		}
		document = DocumentHelper.createDocument();
		
		root = document.addElement(node);
		root.addAttribute("id", "-1");
		root.addAttribute("label", rootName);
		root.addAttribute("type","menu");
		root.addAttribute("menuId","-1");
		if(selFlag!=""){
			root.addAttribute("selected",selFlag);
		}
	}
/*	public static void main(String[] args) {
		GenerateXML generateXML = new GenerateXML();
		IData dataInfo = new DataMap();
		dataInfo.put("intID", "1");
		dataInfo.put("parentID", "0");
		dataInfo.put("departName", "江苏省");
		dataInfo.put("departCode", "1001");
		dataInfo.put("remark", "测试数据");
		generateXML.addChild(generateXML.getRoot(),dataInfo);
		String tempxml = generateXML.createXMLFile();
		System.out.println(tempxml);
	}*/
	
	/**
	 * 产生XML文件
	 * @return 
	 * @throws IOException
	 */
	public String createXMLFile(){
		if(document!=null){
			//return document.asXML().replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "").trim();
			return document.asXML();
		}
		return null;
	}
	/**
	 * 提供给机构树使用
	 * @param rootEle
	 * @param dataInfo
	 * @return
	 */
	public Element addOrgChild(Element rootEle,IData dataInfo){
		Element nodeEle = rootEle.addElement("node");
		nodeEle.addAttribute("id",String.valueOf(dataInfo.get("orgId")));
		nodeEle.addAttribute("pid",String.valueOf(dataInfo.get("parentID")));
		nodeEle.addAttribute("label",String.valueOf(dataInfo.get("orgName",""))+"["+String.valueOf(dataInfo.get("orgCode",""))+"]");
		nodeEle.addAttribute("dcode",String.valueOf(dataInfo.get("orgCode","")));
		nodeEle.addAttribute("orgType",String.valueOf(dataInfo.get("orgType","")));
		nodeEle.addAttribute("orgAddr",String.valueOf(dataInfo.get("orgAddr","")));
		nodeEle.addAttribute("orgLeader",String.valueOf(dataInfo.get("orgLeader","")));
		nodeEle.addAttribute("contactPhone",String.valueOf(dataInfo.get("contactPhone","")));
		nodeEle.addAttribute("mobile",String.valueOf(dataInfo.get("mobile","")));
		nodeEle.addAttribute("contMsg",String.valueOf(dataInfo.get("contMsg","")));
		nodeEle.addAttribute("remark",String.valueOf(dataInfo.get("remark","")));
		nodeEle.addAttribute("intOrder",String.valueOf(dataInfo.get("intOrder","")));
		nodeEle.addAttribute("icon", "iconNode");
		return nodeEle;
	}
	
	
	
	
	/**
	 * 提供给角色机构树使用
	 * @param rootEle
	 * @param dataInfo
	 * @return
	 */
	public Element addOrgRoleChild(Element rootEle,IData dataInfo){
		Element nodeEle = rootEle.addElement("node");
		nodeEle.addAttribute("id",String.valueOf(dataInfo.get("orgId")));
		nodeEle.addAttribute("orgCode",String.valueOf(dataInfo.get("orgCode")));
		nodeEle.addAttribute("label",String.valueOf(dataInfo.get("orgName","")));
		nodeEle.addAttribute("type","ORG");
		nodeEle.addAttribute("icon", "iconNode");
		return nodeEle;
	}
	
	
	/**
	 * 将角色添加到公司下面
	 * @param rootEle
	 * @param dataInfo
	 * @return
	 */
	public Element addRoleChild(Element rootEle,IData dataInfo){
		Element nodeEle = rootEle.addElement("node");
		nodeEle.addAttribute("id",String.valueOf(dataInfo.get("intID")));
		nodeEle.addAttribute("orgCode",String.valueOf(dataInfo.get("orgCode")));
		nodeEle.addAttribute("label",String.valueOf(dataInfo.get("roleName","")));
		nodeEle.addAttribute("roleCode",String.valueOf(dataInfo.get("roleCode","")));
		nodeEle.addAttribute("roleRemark",String.valueOf(dataInfo.get("roleRemark","")));
		nodeEle.addAttribute("type",String.valueOf("ROLE"));
		nodeEle.addAttribute("icon", "iconNode");
		return nodeEle;
	}
	
	
	
	/**
	 * 提供给机构树使用
	 * @param rootEle
	 * @param dataInfo
	 * @return
	 */
	public Element addRoleMenuChild(Element rootEle,IData dataInfo){
		Element nodeEle = rootEle.addElement("node");
		nodeEle.addAttribute("roleId", String.valueOf(dataInfo.get("roleId")));
		nodeEle.addAttribute("menuId", String.valueOf(dataInfo.get("menuId")));
		nodeEle.addAttribute("menuName", String.valueOf(dataInfo.get("menuName")));
		nodeEle.addAttribute("rightCode", String.valueOf(dataInfo.get("rightCode")));
		nodeEle.addAttribute("rightName", String.valueOf(dataInfo.get("rightName")));
		nodeEle.addAttribute("isSel", String.valueOf(dataInfo.get("isSel")));
		return nodeEle;
	}

	
	public Element addMenuChild(Element rootEle,IData dataInfo,List role2menuList){
		Element nodeEle = rootEle.addElement("node");
		nodeEle.addAttribute("id",String.valueOf(dataInfo.get("intID")));
		nodeEle.addAttribute("label",String.valueOf(dataInfo.get("menuName")));
		nodeEle.addAttribute("type","menu");
		if(isMenuSelected(String.valueOf(dataInfo.get("intID")),role2menuList)){
			nodeEle.addAttribute("selected","true");
		}
		if(dataInfo.get("btnDesc")!=null){
			JSONArray menuArr = JSONArray.fromObject(dataInfo.get("btnDesc"));
			for (int j = 0; j < menuArr.size(); j++) {
				Element nodeEle1 = nodeEle.addElement("node");
				JSONObject jsonObj = menuArr.getJSONObject(j);
				nodeEle1.addAttribute("id",String.valueOf(jsonObj.get("id")));
				nodeEle1.addAttribute("label",String.valueOf(jsonObj.get("label")));
				nodeEle1.addAttribute("type","func");
				nodeEle1.addAttribute("menuId",String.valueOf(dataInfo.get("intID")));
				nodeEle1.addAttribute("menuName",String.valueOf(dataInfo.get("menuName")));
				if(isFuncSelected(String.valueOf(jsonObj.get("id")),String.valueOf(dataInfo.get("intID")),role2menuList)){
					nodeEle1.addAttribute("selected","true");
				}
			}
		}
		return nodeEle;
	}
	
	private boolean isFuncSelected(String rightCode,String menuId,List role2menuList){
		boolean isFlag=false;
		if(role2menuList!=null){
			for(int i=0;i<role2menuList.size();i++){
				  IData dataMap=(DataMap)role2menuList.get(i);
				  if(rightCode.equals(dataMap.get("rightCode")) && menuId.equals(dataMap.get("menuId"))){
					  isFlag = true;
					  break;
				  }
				}
		}
		return isFlag;
	}

	
	
	private boolean isMenuSelected(String id,List role2menuList){
		boolean isFlag=false;
		if(role2menuList!=null){
			for(int i=0;i<role2menuList.size();i++){
				  IData dataMap=(DataMap)role2menuList.get(i);
				  if(id.equals(dataMap.get("menuId"))){
					  isFlag = true;
					  break;
				  }
				}
		}
		return isFlag;
	}
	public Element getRoot() {
		return root;
	}

	public void setRoot(Element root) {
		this.root = root;
	}
	
	
}
